<?php

namespace App\Service;

use App\Model\VoteItemModel;
use App\Model\VoteLogModel;
use App\Model\VoteModel;
use EasySwoole\Mysqli\Exception\Exception;
use Throwable;

class VoteService extends BaseService
{
    /**
     * @return array
     * @throws Exception
     * @throws \EasySwoole\ORM\Exception\Exception
     * @throws Throwable
     */
    function getVoteStatus(): array
    {
        $status = VoteModel::create()->where(['is_open' => 0, 'nid' => 0])->all();

        if (!empty($status)) {
            $data = [];
            foreach ($status as $item) {
                $data[] = [
                    'id' => $item['id'],
                    'name' => $item['name'],
                    'span' => $item['span'],
                    'description' => $item['description'],
                    'create_time' => $item['create_time'],
                    'child' => $this->vote_item($item['id'])
                ];
            }
            return [
                'result' => true,
                'msg' => '获取成功',
                'data' => $data
            ];
        }

        return [
            'result' => false,
            'msg' => '获取失败'
        ];
    }

    /**
     * @param $nid
     * @return array|false
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    function vote_item($nid)
    {
        $status = VoteModel::create()->where(['is_open' => 0, 'nid' => $nid])->all();
        if (empty($status)) return false;
        $data = [];

        foreach ($status as $item) {
            $data[] = [
                'id' => $item['id'],
                'name' => $item['name'],
                'span' => $item['span'],
                'description' => $item['description'],
                'create_time' => $item['create_time'],
                'child' => $this->vote_item($item['id'])
            ];
        }

        return $data;
    }

    /**
     * @param $qid
     * @return array
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    function getVoteItem($qid): array
    {
        $votes = VoteItemModel::create()->where(['qid' => $qid, 'is_deleted' => 0])->order('sort', 'asc')->all();
        if (empty($votes)) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }

        $data = [];
        foreach ($votes as $vote) {
            $data[] = $vote;
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }

    /**
     * @param $uid
     * @param $vid
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    function vote($uid, $vid): array
    {
        $start = strtotime('2023-1-15 11:30:00');
        $end = strtotime('2023-1-15 15:00:00');
        $time = time();
        if ($time < $start) {
            return [
                'result' => false,
                'msg' => '投票活动暂未开始【11：:30-15：:00开始】!'
            ];
        }
        if($time > $end){
            return [
                'result' => false,
                'msg' => '投票活动已结束【11：:30-15：:00开始】!'
            ];
        }
        $item = VoteItemModel::create()->getVote(['id' => $vid]);
        if (!$item) {
            return [
                'result' => false,
                'msg' => '错误数据'
            ];
        }

        $qid = $item->qid;

        $log = VoteLogModel::create()->getVote(['uid' => $uid, 'qid' => $qid]);
        if ($log) {
            return [
                'result' => false,
                'msg' => '已投票'
            ];
        }

        $par = [
            'uid' => $uid,
            'qid' => $qid,
            'vid' => $vid
        ];

        $res = VoteLogModel::create()->addVote($par);
        if (!$res) {
            return [
                'result' => false,
                'msg' => '操作失败，请稍后重试'
            ];
        }

        $upRes = VoteItemModel::create()->where(['id' => $vid])->update(['num' => $item->num + 1]);

        if (!$upRes) {
            return [
                'result' => false,
                'msg' => '操作失败'
            ];
        }
        $data = $item->toArray();
        $data['num'] = $data['num']++;
        return [
            'result' => true,
            'msg' => '投票成功',
            'data' => $data
        ];
    }

    /**
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    function rank(): array
    {
        $status = VoteModel::create()->where(['is_open' => 0])->all();
        if (empty($status)) {
            return [
                'result' => false,
                'msg' => '暂无数据'
            ];
        }

        $data = [];

        foreach ($status as $state) {
            $item = VoteItemModel::create()->order('num', 'DESC')->where(['qid' => $state['id'], 'is_deleted' => 0])->all();
            foreach ($item as $v) {
                $data[$state['id']][] = [
                    'vote_name' => $state['name'],
                    'vote_span' => $state['span'],
                    'vote_description' => $state['description'],
                    'name' => $v['name'],
                    'avatar' => $v['avatar'],
                    'span' => $v['span'],
                    'description' => $v['description'],
                    'num' => $v['num']
                ];
            }
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => $data
        ];
    }
}